<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>添加地址</title>
    <meta name="keywords" content="">
    <meta name="description" content="">
    <meta name="renderer" content="webkit">
    <meta http-equiv="Cache-Control" content="no-siteapp"/>
    <meta name="mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta name="format-detection" content="telephone=no,email=no,address=no"/>
    <meta name="viewport" content="width=device-width,minimum-scale=1.0,maximum-scale=1.0,user-scalable=no">
    <link rel="stylesheet" href="../css/zy_common.css"/>
    <link rel="stylesheet" href="../css/zy_base.css"/>
    <link rel="stylesheet" href="../js/LArea-master/LArea.min.css">
    <style>
        .area_ctrl{
            background-color:#fff;
        }
        .area_btn{
            color: #555;
        }
    </style>
</head>
<body>
<div class="bw font15">
    <div class="flex_sb plr24 bdb">
        <span class="col3">收货人</span>
        <input type="text" placeholder="请输入收货人" class="h100 col9 font15 tae" id="name">
    </div>
    <div class="flex_sb plr24 bdb">
        <span class="col3">联系电话</span>
        <input type="tel" placeholder="请输入联系电话" class="h100 col9 font15 tae" oninput="this.value = this.value.replace(/\D/g,'')" id="phone">
    </div>
    <div class="flex_sb plr24 bdb">
        <span class="col3">省/市/区</span>
        <em class="flex_a">
            <input type="text" placeholder="请选择" class="h100 col9 font15 tae mr20" readonly id="city">
            <input id="cityValue" type="text" hidden value=""/>
            <img src="../img/arrow_right.png" class="arrow_r" alt="">
        </em>
    </div>
    <div class="plr24 ptb30">
        <p class="col3 mb10">详细地址</p>
        <p>
            <input type="text" placeholder="请输入详细地址" class="col9 font15 w100" id="addr">
        </p>
    </div>
</div>
<div class="tab_item mt20 bw flex_sb plr24" style="display: none;">
    <div class="font15 col3">设为默认</div>
    <div class="onOff_box flex_a h100">
        <label>
            <input class="mui-switch mui-switch-animbg" type="checkbox" name="onOff"  onchange="onOffs(this)"  id="onOff" >
        </label>
    </div>
</div>
<div class="confirm" style="margin-top: .6rem">保存</div>
</body>
</html>
<script type="text/javascript" src="../js/jquery.min.js"></script>
<script type="text/javascript" src="../js/dlc.js"></script>
<script src="../js/LArea-master/cityData.js"></script>
<script src="../js/LArea-master/LArea.js"></script>
<script>
    var status=1,onOff=0;
    $(function () {
        if(getUrlParam('addressId')){//编辑
            var data=getLocalData('addr_data');
            $('#name').val(data.name);
            $('#phone').val(data.phone);
             $('#addr').val(data.addr);
            $('#city').val(data.province);
            if(data.is_default==1){//默认地址
               $('#onOff').attr('checked',true);
                status=1;
            }else{
                $('.tab_item').css('display','flex');
                $('#onOff').attr('checked',false);
                status=0;
            }
        }else{//添加
            $('.tab_item').css('display','flex');
            $('#onOff').attr('checked',true);
            status=1;
        }

        //地址选择
        area('#city','#cityValue');
        //保存

       $('.confirm').click(function () {
          console.log(status)
           var name = $('#name').val();
           var phone = $('#phone').val();
           var addr = $('#addr').val();
           var city = $('#city').val();//地址
           var cityValue=$('#cityValue').val();//地址id
           console.log(cityValue)
           if(isNull(name)){
               dlctipbox.show('请输入收货人');
           }else if(isNull(phone)){
               dlctipbox.show('请输入联系电话');
           }else if(!checkMobileAndTel(phone)){
               dlctipbox.show('联系电话有误');
           }else if(isNull(city)){
               dlctipbox.show('请选择省市区');
           }else if(isNull(addr)){
               dlctipbox.show('请输入详细地址');
           }else{
               if(onOff)return false;
               onOff=1;
               dlctipbox.loading('请稍后');
               var data={
                   name:name,
                   phone:phone,
                   province:city,
                   addr:addr,
                   isDefault:status
               };
               if(getUrlParam('addressId')){//编辑
                   data.addressId=getUrlParam('addressId');
               }
               dlc_request('/api/address/saveOrUpdate',data,function (res) {
                   console.log(res);
                   dlctipbox.clear();
                   if(res.code==1){
                        dlctipbox.alert('操作成功',function (flag) {
                            if(flag==1){
                                history.go(-1);
                            }
                        });
                   }else{
                       dlctipbox.show(res.msg);
                   }
               })
           }
       }) ;

    });

    function onOffs(obj){
        if($(obj).is(':checked')){
            status=1;
        }else{
            status=0;
        }
    }

    //地址选择
    function area(id,valueId) {
        var area = new LArea();
        area.init({
            'trigger':id , //触发选择控件的文本框，同时选择完毕后name属性输出到该位置
            'valueTo': valueId, //选择完毕后id属性输出到该位置
            'keys': {
                id: 'id',
                name: 'name'
            }, //绑定数据源相关字段 id对应valueTo的value属性输出 name对应trigger的value属性输出
            'type': 1, //数据源类型
            'data': cityData //数据源
        });
    }
</script>